package drds.server.sqlparser.sqlobject.statement.insert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import drds.server.sqlparser.sqlobject.AbstractSqlObject;
import drds.server.sqlparser.sqlobject.expression.Expression;
import drds.server.sqlparser.visitor.Visitor;

/**
 * 
 * @author 曹正辉<330937205@qq.com>
 * 
 */
public class ValueList extends AbstractSqlObject {
	private final List<Expression> valueList;
	private Map<String, Object> columnNameMap = new HashMap<String, Object>();

	public ValueList() {
		this(new ArrayList<Expression>());
	}

	public ValueList(List<Expression> valueList) {
		this.valueList = valueList;
		for (int i = 0; i < valueList.size(); ++i) {
			valueList.get(i).setParent(this);
		}
	}

	public void addValue(Expression value) {
		value.setParent(this);
		valueList.add(value);
	}

	public List<Expression> getValueList() {
		return valueList;
	}

	/**
	 * 整个系统最好的设计之一
	 */
	public void setColumnNameMap(List<Expression> columnNameList) {

		int size = columnNameList.size();
		for (int i = 0; i < size; i++) {
			String columnName = columnNameList.get(i).toString();
			this.columnNameMap.put(columnName, valueList.get(i));
		}
	}

	public Map<String, Object> getColumnNameMap() {
		return columnNameMap;
	}

	public void output(StringBuffer sb) {
		sb.append(" values (");
		for (int i = 0, size = valueList.size(); i < size; ++i) {
			if (i != 0) {
				sb.append(", ");
			}
			valueList.get(i).output(sb);
		}
		sb.append(")");
	}

	@Override
	protected void accept0(Visitor visitor) {
		if (visitor.visit(this)) {
			this.acceptChild(visitor, valueList);
		}

		visitor.endVisit(this);
	}

}
